//
//  GuideViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/8.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "GuideViewController.h"
#import "LoginViewController.h"
#import "BLHeader.h"
#import <Masonry.h>

@interface GuideViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *guideImages;

@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
}

- (void) setupUI {
    //self.edgesForExtendedLayout = UIRectEdgeNone;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.itemSize = CGSizeMake(KScreenWidth, KScreenHeight);
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ReuseIdentify];
    self.guideImages = @[@"guide1", @"guide2", @"guide3"];
    
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.guideImages.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseIdentify forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIImageView *imageView = [[UIImageView alloc] init];
    [cell.contentView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.equalTo(cell.contentView);
    }];
    imageView.image = [UIImage imageNamed:self.guideImages[indexPath.item]];
    
    if (indexPath.item == 2) {
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHideSelf:)];
        
         UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHideSelf:)];
        
         UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHideSelf:)];
        
         UISwipeGestureRecognizer *swipe4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHideSelf:)];
        
        
        swipe.direction = UISwipeGestureRecognizerDirectionUp;
        swipe2.direction = UISwipeGestureRecognizerDirectionDown;
        swipe3.direction = UISwipeGestureRecognizerDirectionLeft;
        swipe4.direction = UISwipeGestureRecognizerDirectionRight;
        cell.userInteractionEnabled = YES;
        [cell addGestureRecognizer:swipe];
        [cell addGestureRecognizer:swipe2];
        [cell addGestureRecognizer:swipe3];
        [cell addGestureRecognizer:swipe4];
    }
    
    return cell;
}

- (void)swipeHideSelf:(UISwipeGestureRecognizer *)swipe {
//    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
//        NSLog(@"right");
//    }else if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
//        NSLog(@"left");
//    }else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
//        NSLog(@"up");
//    }else {
//        NSLog(@"down");
//    }
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSInteger currentPage = targetContentOffset->x / KScreenWidth;
    self.pageControl.currentPage = currentPage;
    
    NSLog(@"x = %f", velocity.x);
    if (currentPage == self.pageControl.numberOfPages - 1 && velocity.x >= 0) {
        [self.view removeFromSuperview];
    }
}




@end
